﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using bdxyit.com.RecruitmentAppSys.DBUtility;
using System.Data;
using System.Data.SqlClient;
/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：ComputerLevelModels
*系统时间：3/25/2012 3:33:51 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:33:51 PM
*修改事件：
***/

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class ComputerLevelDAL : IComputerLevelDAL
    {
        private const string SQL_SELECT_COMPUTERLEVEL = " select LevelID,LevelName,Type,SysID from ComputerLevels ";
        private const string SQL_SELECT_COMPUTERLEVEL_BY_LEVELID = " select  top 1 LevelID,LevelName,Type,SysID from ComputerLevels "
                                                                    + "  where  LevelID=@LevelID";
        private const string SQL_DELETE_COMPUTERLEVEL_BY_LEVELID = " delete from ComputerLevels "
                                                                    + "  where  LevelID=@LevelID";
        private const string SQL_UPDATE_COMPUTERLEVEL_BY_LEVELID = " update ComputerLevels set "
                                                                + " LevelID=@LevelID,"
                                                                + " LevelName=@LevelName,"
                                                                + " Type=@Type,"
                                                                + " SysID=@SysID"
                                                                + "  where  LevelID=@LevelID";
        private const string SQL_INSERT_COMPUTERLEVEL = " insert into ComputerLevels("
                                                        + " LevelID,LevelName,Type,SysID)"
                                                        + "  values ("
                                                        + " @LevelID,@LevelName,@Type,@SysID)";
        private const string FIELD_LevelID = "@LevelID";
        private const string FIELD_LevelName = "@LevelName";
        private const string FIELD_Type = "@Type";
        private const string FIELD_SysID = "@SysID";
        public bool InsertComputerLevel(ComputerLevel cmodel)
        {
            bool flag = false;
            SqlParameter[] ComputerLevelParms = GetComputerLevelParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句

            ComputerLevelParms[0].Value = cmodel.LevelID;
            ComputerLevelParms[1].Value = cmodel.LevelName;
            ComputerLevelParms[2].Value = cmodel.Type;
            ComputerLevelParms[3].Value = cmodel.SysID;
            foreach (SqlParameter parm in ComputerLevelParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_COMPUTERLEVEL;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteComputerLevel(string LevelID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_LevelID, SqlDbType.VarChar, 10);
            parm.Value = LevelID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_COMPUTERLEVEL_BY_LEVELID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateComputerLevel(ComputerLevel cmodel)
        {
            bool flag = false;
            SqlParameter[] ComputerLevelParms = GetComputerLevelParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            ComputerLevelParms[0].Value = cmodel.LevelID;
            ComputerLevelParms[1].Value = cmodel.LevelName;
            ComputerLevelParms[2].Value = cmodel.Type;
            ComputerLevelParms[3].Value = cmodel.SysID;

            foreach (SqlParameter parm in ComputerLevelParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_COMPUTERLEVEL_BY_LEVELID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public ComputerLevel SelectComputerLevelByLevelID(string LevelID)
        {
            ComputerLevel cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_LevelID, SqlDbType.VarChar, 10);
            parm.Value = LevelID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_COMPUTERLEVEL_BY_LEVELID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new ComputerLevel();
                    if (dr.HasRows == true)
                    {
                        if (dr["LevelID"] != null && dr["LevelID"].ToString() != "")
                        {
                            cmodel.LevelID = dr["LevelID"].ToString();
                        }
                        if (dr["LevelName"] != null && dr["LevelName"].ToString() != "")
                        {
                            cmodel.LevelName = dr["LevelName"].ToString();
                        }
                        if (dr["Type"] != null && dr["Type"].ToString() != "")
                        {
                            cmodel.Type = dr["Type"].ToString();
                        }
                        if (dr["SysID"] != null && dr["SysID"].ToString() != "")
                        {
                            cmodel.SysID = dr["SysID"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<ComputerLevel> SelectComputerLevel()
        {
            List<ComputerLevel> selectlist = new List<ComputerLevel>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_COMPUTERLEVEL))
            {
                while (dr.Read())
                {
                    ComputerLevel cmodel = new ComputerLevel();
                    //写添赋值语句

                    if (dr["LevelID"] != null && dr["LevelID"].ToString() != "")
                    {
                        cmodel.LevelID = dr["LevelID"].ToString();
                    }
                    if (dr["LevelName"] != null && dr["LevelName"].ToString() != "")
                    {
                        cmodel.LevelName = dr["LevelName"].ToString();
                    }
                    if (dr["Type"] != null && dr["Type"].ToString() != "")
                    {
                        cmodel.Type = dr["Type"].ToString();
                    }
                    if (dr["SysID"] != null && dr["SysID"].ToString() != "")
                    {
                        cmodel.SysID = dr["SysID"].ToString();
                    }
                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetComputerLevelParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
                	new SqlParameter(FIELD_LevelID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_LevelName, SqlDbType.VarChar,50),
					new SqlParameter(FIELD_Type, SqlDbType.VarChar,20),
					new SqlParameter(FIELD_SysID, SqlDbType.VarChar,10)};
            return parms;
        }

    }
}